<?php 
/* Begin LFCC Employee Directory for WordPress code
 *
 * Place this anywhere in your functions.php file in your theme. - RMD 4/21/2014
 *
 * (C) Copyright Lord Fairfax Community College released under GPL v2 or later
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/*
 * This filter and function creates the title using the First and Last Name of the Employee
 */
add_filter('title_save_pre', 'cpt_employee_generate_the_title'); 
function cpt_employee_generate_the_title($content, $post_id) {
        global $post;
        if ($post->post_type=='employee') {
                $first_name = get_post_meta($post->ID, 'first_name', true);
                $last_name = get_post_meta($post->ID, 'last_name', true);
                $content = $first_name.' '.$last_name;
        }
        return $content;
}

/*
 * This filter and function creates the slug by using the title generated above.
 */
add_filter('wp_insert_post_data', 'cpt_employee_generate_the_slug', 99, 2);
function cpt_employee_generate_the_slug($data, $postarr) {
        if ($data['post_type']=='employee') {
                $data['post_name'] = sanitize_title( $data['post_title'] );
        }
        return $data;

}

// End LFCC Employee Directory for WordPress code
?>